#!/usr/bin/perl -w 
# 20 CUDA benchmarks, Inti
# 13 rodinia benchmarks, Titan
# 6  parboil benchmarks, Inti
# 39 benchmarks in total

use English;

@myorder = ("SAD", "PVC", "SSC", "BFS", "MUM", "CFD", "KMN", "SCP", "FWT", "IIX", "SPMV", "JPEG", "BFSR", "SC", "FFT", "SD2", "WP", "PVR", "BP", "LIB", "CON", "AES","SD1","BLK", "HS", "SLA","DN", "LPS", "NN", "PFN", "LYTE", "LUD", "MM", "STO", "CP", "NQU", "CUTP", "HW", "TPAF");

@mylist = ("parboil/sad/output_sad.txt", "Mars/PageViewCount/output_PVC.txt", "Mars/SimilarityScore/output_SSC.txt", "CUDA/BFS/output_BFS.txt", "CUDA/MUM/output_MUM.txt", "rodinia/cfd/output_cfd.txt", "CUDA/kmeans/output_kmeans.txt", "CUDA/SCP/output_SCP.txt",  "CUDA/FWT/output_FWT.txt","Mars/InvertedIndex/output_IIX.txt","parboil/spmv/output_spmv.txt", "CUDA/JPEG/output_JPEG.txt",  "rodinia/bfs/output_bfs.txt", "rodinia/streamcluster/output_streamcluster.txt", "parboil/fft/output_fft.txt", "rodinia/srad_v2/output_srad_v2.txt", "CUDA/WP/output_WP.txt",
"Mars/PageViewRank/output_PVR.txt",
"rodinia/backprop/output_backprop.txt", "CUDA/LIB/output_LIB.txt", "CUDA/CON/output_CON.txt","CUDA/AES/output_AES.txt","rodinia/srad_v1/output_srad_v1.txt","CUDA/BlackScholes/output_BlackScholes.txt", "rodinia/hotspot/output_hotspot.txt", "CUDA/SLA/output_SLA.txt",  "CUDA/denoise/output_denoise.txt","CUDA/LPS/output_LPS.txt", "CUDA/NN/output_NN.txt","rodinia/pf_naive/output_pf_naive.txt", "rodinia/leukocyte/output_leukocyte.txt", "rodinia/lud/output_lud.txt","parboil/mm/output_mm.txt", "CUDA/STO/output_STO.txt", "CUDA/CP/output_CP.txt", "CUDA/NQU/output_NQU.txt", "parboil/cutcp/output_cutcp.txt", "rodinia/heartwall/output_heartwall.txt","parboil/tpacf/output_tpacf.txt");

@file_list = (@mylist);
@benchmarks = (@myorder);
$result_file = "all_extra.txt";

open(OUTPUT, ">$result_file") || die "Cannot open file $result_file for writing\n";

for ($j = 0; $j <= $#file_list; $j++) {  

	my $dmiss = `grep "L1 Data Cache Total Miss Rate" $file_list[$j] | tail -n 1 | awk '\{print \$8\}'`;
	chomp($dmiss);
	push @globaldmiss, $dmiss;

	my $tmiss = `grep "L1 Texture Cache Total Miss Rate" $file_list[$j] | tail -n 1 | awk '\{print \$8\}'`;
	chomp($tmiss);
	push @globaltmiss, $tmiss;

	my $cmiss = `grep "L1 Const Cache Total Miss Rate"  $file_list[$j] | tail -n 1 | awk '\{print \$8\}'`;
	chomp($cmiss);
	push @globalcmiss, $cmiss;	
	
	my $preempt = `grep preempt  $file_list[$j] | tail -n 1 | awk '\{print \$3\}'`;
	chomp($preempt);
	push @globalpreempt, $preempt;	
	
	my $NONPRE = `grep NONPRE  $file_list[$j] | tail -n 1 | awk '\{print \$3\}'`;
	chomp($NONPRE);
	push @globalNONPRE, $NONPRE;	
	
	my $EQPRE = `grep EQPRE  $file_list[$j] | tail -n 1 | awk '\{print \$3\}'`;
	chomp($EQPRE);
	push @globalEQPRE, $EQPRE;	
	
} 
 	# Misses ----------------------------------
	print OUTPUT "Benchmarks \t";
	print OUTPUT "DMISS\t";
	print OUTPUT "TMISS\t";
	print OUTPUT "CMISS\t";
	print OUTPUT "preempt\t";
	print OUTPUT "NONPRE\t";
	print OUTPUT "EQPRE\t";
	print OUTPUT "\n";


for ($k = 0;$k <= $#file_list;$k++) 
{
    # BASELINE Starts ----------------------------------------------------------------
 	# General ----------------------------------
	print OUTPUT "$benchmarks[$k] \t";
	print OUTPUT "$globaldmiss[$k] \t";
	print OUTPUT "$globaltmiss[$k] \t";
	print OUTPUT "$globalpreempt[$k] \t";
	print OUTPUT "$globalNONPRE[$k] \t";
	print OUTPUT "$globalEQPRE[$k] \t";
	print OUTPUT "\n";
}


sub avg {
	@_ == 1 or die ('Sub usage: $avg = avg(\@array);');
	my ($array_ref) = @_;
	my $sum;
	my $count = scalar @$array_ref;
        if ($count == 0) {
                return 0;
        }
	foreach (@$array_ref) { $sum += $_; }
	return $sum / $count;
}

sub max {
	@_ == 1 or die ('Sub usage: $max = max(\@array);');
	my ($array_ref) = @_;
	my $maxval = -999999999999999999;
	foreach (@$array_ref)
	{
		if ( $maxval < $_ )
		{
			$maxval = $_;
		}
	}
	return $maxval;
}

sub sum {
	@_ == 1 or die ('Sub usage: $sum = sum(\@array);');
	my ($array_ref) = @_;
	my $sum;
	foreach (@$array_ref) { $sum += $_; }
	return $sum;
}

